package com.jxzyk.http;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.core.entity.params.MapParams;
import com.core.entity.params.ReqParams;
import com.core.util.T;
import com.core.util.UiTools;
import com.jxzyk.R;
import com.jxzyk.http.HttpTools.OnHttpListener;

public abstract class BaseRequest extends Dialog {
	private Context context;
	private View view;
	private TextView title;
	private HttpTools tools;
	public ReqParams params;

	public void setParams(ReqParams params) {
		this.params = params;
	}

	private OnDialogListener listener;
	private String URL;
	private Query query;

	public Query getQuery() {
		return query;
	}

	public void setQuery(Query query) {
		this.query = query;
	}

	public String getURL() {
		return URL;
	}

	public void setOnDialogListener(OnDialogListener listener) {
		this.listener = listener;
	}

	public OnDialogListener getDialogListener() {
		return this.listener;
	}

	public interface OnDialogListener {
		void onSuccess(JSONObject json);

		void onFailure(String msg);
	}

	public void setParams(String... value) {

	}

	public abstract String getUrl();

	public abstract ReqParams getParams();

	public abstract OnDialogListener getListener();

	public BaseRequest(Context context) {
		super(context);
		initContext(context, null);
	}

	public BaseRequest(Context context, String url) {
		super(context);
		initContext(context, url);
	}

	private void initContext(Context context, String url) {
		this.context = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.progress_dialog);
		title = (TextView) findViewById(R.id.title);
		setCanceledOnTouchOutside(false);
		tools = new HttpTools();
		params = new MapParams();
		OnDialogListener dialogListener = getListener();
		if (dialogListener != null) {
			listener = dialogListener;
		}
		if (url != null) {
			this.URL = url;
		}
	}

	public void start() {
		String url = getUrl();
		ReqParams params = getParams();
		if (this.params != null) {
			params = this.params;
		}
		String msg = "";
		if (url == null || params == null || listener == null) {
			msg = "请求失败,请检查参数";
			T.toast(msg);
			listener.onFailure(msg);
			dismiss();
			return;
		}
		if (!UiTools.checkNet(context)) {
			msg = "网络未连接，请检查网络设置";
			T.toast(msg);
			listener.onFailure(msg);
			dismiss();
			return;
		}
		tools.post(url, params, new OnHttpListener() {

			@Override
			public void onSuccess(JSONObject json) {
				int result = json.getInteger("result");
				dismiss();
				switch (result) {
				case 1:
					listener.onSuccess(json);
					break;
				case 0:
					String message = json.getString("message");
					if (!TextUtils.isEmpty(message)) {
						T.toast(message);
					}
					listener.onFailure(message);
					break;
				default:
					break;
				}

			}

			@Override
			public void onFailure() {
				dismiss();
				listener.onFailure(null);
			}
		});
	}

	public void show(String msg) {
		title.setText(msg);
		show();
	}

	@Override
	public void show() {
		super.show();
		start();
	}
}
